package controller;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.comm.Bluetooth;
import lejos.nxt.comm.NXTConnection;

public class BluetoothController {
	
	private NXTConnection connection;
    private static final String CONNECTED = "Connected";
    private static final String WAITING = "Waiting...";
    private static final String CLOSING = "Closing...";
    private DataInputStream dis;
    private DataOutputStream dos;
	
	public void connect() {
		Controller.writeToScreen(WAITING);
		connection = Bluetooth.waitForConnection();
		//Controller.limpaTela();
		Controller.writeToScreen(CONNECTED);
		dis = connection.openDataInputStream();
		dos = connection.openDataOutputStream();
	}
	
	public void disconnect() {
		Controller.writeToScreen(CLOSING);
		connection.close();
	}
	
	public char[] getMessage() throws IOException {
		char[] cmd = new char [10];
		int count = 0;
		char c;
		
		do {
            c = (char) dis.readByte(); // Le um byte
            cmd[count] = c; // Adiciona ao vetor
			count++;
		} while (c != '\n');
		
		return cmd;
	}
	
	public int getMessageParam() throws IOException {
		int param = 0;
		
		param = dis.readInt();
		
		return param;
	}
	
	public void sendMessage(float param) throws IOException {
		dos.writeFloat(param);
		dos.flush();
	}
	
	public void sendMessage(int param) throws IOException {
		dos.writeInt(param);
		dos.flush();
	}
}
